<script setup lang="ts">
import type { SysPostQo } from '#/system/post'
import { createSysPostApi, getSysPostDetailsApi, updateSysPostApi } from '@/api/system/post'

interface OnCloseResolveType extends CloseResolveType {}
let onCloseResolve = function (_value: OnCloseResolveType) { }

const dialogRef = $ref<DialogInstance>()
const ruleFormRef = ref<FormInstance>()
let dialogModal = $ref<SysPostQo>({})
let dialogAction = $ref<DialogAction>('add')
let confirmLoading = $ref(false)
let dialogLoading = $ref(false)

const rules = reactive<FormRules<SysPostQo>>({
  name: [
    { required: true, message: '请输入岗位名称', trigger: 'blur' },
  ],
  code: [
    { required: true, message: '请输入岗位编码', trigger: 'blur' },
  ],
})
async function getDefaultModel() {
  return {} as SysPostQo
}
// 打开
async function showDialog(args: {
  title: string
  action?: DialogAction
  model: {
    id?: string
  }
}) {
  const dialogRes = dialogRef!.openDialog<OnCloseResolveType>({
    title: args.title,
  })

  ruleFormRef.value?.clearValidate()
  onCloseResolve = dialogRes.onCloseResolve

  dialogAction = args.action ?? 'add'

  if (args.model.id) {
    dialogModal.id = args.model.id
  }

  if (args.action === 'add') {
    const defaultModel = await getDefaultModel()
    dialogModal = {
      ...defaultModel,
      ...args.model,
    }
  }
  else if (args.action === 'edit' || args.action === 'views') {
    await reflash()
  }

  return await dialogRes.dialogPrm
}

async function reflash() {
  if (!dialogModal.id)
    return
  dialogLoading = true
  const res = await getSysPostDetailsApi(dialogModal.id)
  if (!res)
    return
  dialogLoading = false
  Object.assign(dialogModal, res.data)
}

// 确认
async function confirm(formEl: FormInstance | undefined) {
  if (!formEl)
    return
  await formEl.validate(async (valid) => {
    if (!valid) {
      return
    }
    confirmLoading = true
    if (dialogAction === 'add') {
      await createSysPostApi(dialogModal).finally(() => {
        confirmLoading = false
      })
    }
    else if (dialogAction === 'edit') {
      await updateSysPostApi(dialogModal).finally(() => {
        confirmLoading = false
      })
    }
    confirmLoading = false
    Message({
      type: 'success',
      message: `${DialogActionMap[dialogAction as DialogAction]}成功`,
    })
    onCloseResolve({ type: true })
  })
}

// 取消
function cancel() {
  ruleFormRef.value?.clearValidate()
  onCloseResolve({ type: false })
}

defineExpose({ showDialog })
</script>

<template>
  <sy-dialog
    ref="dialogRef"
    :before-close="cancel"
    :hide-confirm="dialogAction === 'views'"
    :confirm-loading="confirmLoading"
    :disabled-confirm="dialogLoading"
    @confirm="confirm(ruleFormRef)"
  >
    <el-form
      ref="ruleFormRef"
      v-loading="dialogLoading"
      :model="dialogModal"
      :rules="rules"
      label-width="auto"
      status-icon
    >
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="dialogModal.code"
          placeholder="请输入岗位编码"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="dialogModal.name"
          placeholder="请输入岗位名称"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="dialogModal.remark"
          type="textarea"
          placeholder="请输入备注"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
    </el-form>
  </sy-dialog>
</template>

<style scoped></style>
